﻿using HelloMinasan.Web.Song.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Domain;
using HelloMinasan.Business.Extend;

namespace HelloMinasan.Web.Song.Controllers
{
    public class SongBankTestController : Controller
    {
        //private IRepository._SongBankTestIRepository IRepositorySong;
        //public SongBankTestController()
        //    : this(new SongBankTestRepository())
        //{
        //}

        //public SongBankTestController(IRepository._SongBankTestIRepository IRepositorySong)
        //{
        //    // TODO: Complete member initialization
        //    this.IRepositorySong = IRepositorySong;
        //}

        private ISongService songService;
        private IUserService userService;

        public SongBankTestController(ISongService songService, IUserService userService)
        {
            this.songService = songService;
            this.userService = userService;
        }

        //sẽ nhận biến songid nữa
        [Authorize]
        public ActionResult CreateBankTest(int? Songid)
        {
            int ID = Songid == null? 0:Songid.Value;// 2;
            SongBankTestViewModel Viewmodel = new SongBankTestViewModel();
            var model = songService.Get_SongLyric(ID);
            Viewmodel.LyricContent = model.LyricContent;
            return View(Viewmodel);
        }

        [HttpPost, ValidateInput(false)]
        [Authorize]
        public ActionResult CreateBankTest(SongBankTestViewModel lyric)
        {
            QuestionBlank ly = new QuestionBlank();

            //ly.SongId = lyric.SongId;
            //ly.SongId = 2;
            ly.BlankContent = lyric.LyricContent;
            ly.PostedDate = DateTime.Now;
            songService.Save_SongLyric(ly);
            return View();
        }

        [Authorize]
        public ActionResult TestBank(int? songID)
        {
            int songid = songID == null? 0: songID.Value;// 2;

            QuestionBlank question = new QuestionBlank();
            question = songService.Get_QuestionBank(songid);
            //
            var ly = question.BlankContent;
            //tìm tất cả các giá trị làm trong thẻ này
            string dk = "<span style=\"font-weight: bold;\">[^>]+";
            Regex r = new Regex(dk);
            MatchCollection m = r.Matches(ly);
            //đếm giá trị của từng biến
            int[] mangindex = new int[m.Count];
            int index;
            StringBuilder sb = new StringBuilder(ly);
            int i = 0;
            while (ly.IndexOf("font-weight: bold") != -1)
            {
                //lấy vị trí của <span>font-weight: bold
                index = ly.IndexOf("font-weight: bold") - 13; // -20
                //xóa
                sb.Remove(index, m[i].Length + 1);
                //sb.Insert(index, "@Html.TextBox(\"i\")");
                mangindex[i] = index;
                sb.Insert(index, " ");
                ly = sb.ToString();
                i++;
            }
            question.BlankContent = ly;
            ViewData["MangIdex"] = mangindex;
            return View(question);
        }

    }
}
